//
//  XS_ApplyVC.swift
//  XueDao
//
//  Created by Mac on 2020/12/10.
//

import UIKit

class XS_ApplyVC: XS_BaseVC {

    @IBOutlet weak var collectionView: UICollectionView!
    lazy var dataModel = XS_ApplyItemM()
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        title = "报名"
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: .bottomHeight, right: 0)
        collectionView.register(UINib.init(nibName: "XS_ApplyItemCell", bundle: Bundle.main), forCellWithReuseIdentifier: "XS_ApplyItemCell")
        collectionView.mj_header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(headerRefesh))
        collectionView.mj_footer = MJRefreshBackNormalFooter.init(refreshingTarget: self, refreshingAction: #selector(footerRefesh))
        requestData()
    }
    
    @objc func headerRefesh(){
        requestData(true)
    }
    
    @objc func footerRefesh(){
        requestData(false)
    }
    
    func requestData(_ isFirst: Bool = false){
        if isFirst {
            dataModel.current = 0
            dataModel.records.removeAll()
        }
        XS_NetWork.requestJson("/user/apply", parameters: ["pageNo" : dataModel.current+1, "pageSize" : dataModel.size])
            .mapModel(type: XS_ApplyItemM.self) // 返回 模型数组 和原始数据
            .subscribe(onNext: { [weak self] model, resp in
                guard let `self` = self, resp.success, let model = model else { return }
                model.records = self.dataModel.records + model.records
                self.dataModel = model
                self.collectionView.reloadData()
                self.collectionView.mj_header?.endRefreshing()
                if model.current >= model.pages { // 没有更多
                    self.collectionView.mj_footer?.endRefreshingWithNoMoreData()
                }else{
                    self.collectionView.mj_footer?.endRefreshing()
                }
            }).disposed(by: disposeBag)
    }

}

extension XS_ApplyVC : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataModel.records.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "XS_ApplyItemCell", for: indexPath) as! XS_ApplyItemCell
        if indexPath.item < dataModel.records.count {
            cell.model = dataModel.records[indexPath.item]
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize.init(width: .screenWidth - 32, height: 140)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets.init(top: 16, left: 16, bottom: 16, right: 16)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 16
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 16
    }
}
